package com.jacklin.handler;

import com.jacklin.VO.ResultVO;
import com.jacklin.config.ProjectUrlConfig;
import com.jacklin.exception.MyException;
import com.jacklin.exception.ResponseNewStarSellException;
import com.jacklin.exception.SellerAuthorException;
import com.jacklin.utils.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author 林必昭
 * @Date 2019/4/23 10:52
 */

@ControllerAdvice
public class SellerExceptionHandler {


    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    /**
     * 拦截登陆异常，跳转到登陆页面，使用concat来连接跳转地址字符串
     * 地址：http://newstarsell.natapp1.cc/newstarsell/wechat/qrAuthorized?retrunUrl=http://newstarsell.natapp1.cc/newstarsell/seller/login
     * @return
     */
    @ExceptionHandler(value = SellerAuthorException.class)
    public ModelAndView handlerAuthorizedException(){
        return new ModelAndView("redirect:"
                .concat(projectUrlConfig.getWechatOpenAuthorize()
                        .concat("/newstarsell/wechat/qrAuthorize"))
                        .concat("?retrunUrl=")
                        .concat(projectUrlConfig.getNewstarsell())
                        .concat("newstarsell/seller/login"));
    }

    /**
     * 异常统一处理，这里捕获MyException自定义异常
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ResultVO handlerSellerException(MyException e){
        return ResultVOUtil.error(e.getCode(),e.getMessage());
    }

    /**
     * 需要在请求错误时，返回给前端的是403状态码，而不是200
     * 这里使用@ResponseStatus注解实现
     */
    @ExceptionHandler(value = ResponseNewStarSellException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handlerResponseNewStarSellException(){

    }
}
